package com.tuteng.administrator.ashospital.Manager;


import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by HZY on 2018/1/14.
 */
public class DateUtils {

    public static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

    public static String getCurDataString() {
        return dateFormat.format(new Date());
    }

    /**
     * 比较当前日期和输入日期相差的天数
     * @param date
     * @throws ParseException
     */
    public static int  daysBetween( String date) {
        Date date2=new Date();
        Date date3=null;
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            date2=sdf.parse(sdf.format(date2));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            date3=    sdf.parse(sdf.format(sdf.parse(date)));
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Calendar cal =Calendar.getInstance();
        cal.setTime(date2);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date3);
        long time2 = cal.getTimeInMillis();
        long between_days=(time1-time2)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**获取当前时间格式如2017-08-12 08 23:59.212**/
    public static  String  Getdnowdatexx(){
        Date date1=new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String tablename=dateFormat.format(date1);
        return tablename;
    }

    /**
     * 获取时间格式如20180407
     * @return
     */
    public static String getStringToday(){
        Date currentTimeDate=new Date();
        SimpleDateFormat formater =new SimpleDateFormat("yyyMMdd");
        String dString=formater.format(currentTimeDate);
        return dString;
    }

    /**
     * 通过身份证号获取年龄
     * @param idCard
     * @return
     */
    public static int getAgeByIdCard(String idCard) {
        int iAge = 0;
        Calendar cal = Calendar.getInstance();
        String year = idCard.substring(6, 10);
        int iCurrYear = cal.get(Calendar.YEAR);
        iAge = iCurrYear - Integer.valueOf(year);
        return iAge;
    }

    /**转换时间格式如2017-08-12 08 00**/
    public static  String  converttimetoYYMMDDHH00(String date){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date1 = null;
        try {
            date1=sdf.parse(date);

        } catch (ParseException ex) {
            System.out.print("jycx"+"时间转换有错误");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd 00:00.000");
        String tablename=dateFormat.format(date1);
        return tablename;
    }

    public static  String  converttimetoYYMMDD(String date){

        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
        Date date1 = null;
        try {
            date1=sdf.parse(date);

        } catch (ParseException ex) {
            System.out.print("jycx"+"时间转换有错误");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD");
        String tablename=dateFormat.format(date1);
        return tablename;
    }

    /**
     * 判断输入时间是否等于当日
     * @param ghrq
     * @return
     */
    public static boolean  datesdiferrent(String ghrq) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        Date date1 = null;
        Date date =new Date();
        try {
            date1=sdf.parse(ghrq);

        } catch (ParseException ex) {
            System.out.print("jycx"+"时间转换有错误");
        }
        String Bigdadate=sdf.format(date1);
        String nowdateString=sdf.format(date);
        boolean ok=false;
        if(Bigdadate.equals(nowdateString)){
            ok=true;
        }else{ok=false;}
        return ok;
    }
    /** 
  * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数 
  */public static String getNextDay(String nowdate, String delay) { 
 try{ 
 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String mdate = ""; 
 Date d = strToDate(nowdate); 
 long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24 * 60 * 60; 
 d.setTime(myTime * 1000); 
 mdate = format.format(d); 
 return mdate; 
        }catch(Exception e){ 
 return ""; 
 } 
 } 
 
 /** 
* 将短时间格式字符串转换为时间 yyyy-MM-dd  
 *  
  * @param strDate 
 * @return 
 */
        public static Date strToDate(String strDate) {  
       SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
       ParsePosition pos = new ParsePosition(0);  
       Date strtodate = formatter.parse(strDate, pos);  
       return strtodate;  
        }   
}
